<?php
include_once('prepend.inc.php');
include_once('data_classes/cl_Basket.class.php');
include_once('PeopleWidget.php');
include_once('Panel/pnlHeader.php');

Class ShareAssets extends QForm
{
	protected $dtrAssets;
	protected $dtrPeople;
	protected $dtrBasket;
	protected $pnlLeft;
	protected $pnlRight;
	protected $txtSearchAssets;
	protected $txtSearchPeople;
	protected $btnSearchAssets;
	protected $btnSearchPeople;
	protected $btnLendAssets;
	protected $btnBorrowAssets;
	protected $arrChosenAssets = array();
	protected $arrBasket = array();
	protected $basket = array();
	protected $objBasket;
	protected $lnkEmptyBasket;
	protected $dlgPeopleBox;
	protected $lnkChoosePerson;
	protected $lnkListView;
	protected $lnkCoverView;
	protected $blnListView = true;
	protected $strBorrower;
	protected $pnlHeader;
	
	
	protected function Form_Run()
	{
		QApplicationBase::CheckRemoteAdmin();
		QApplication::$LoadedPage = 'Lend my things';
	}
	
	protected function Form_Create()
	{
		QApplication::$LoadedPage = 'Lend my things';		
		$this->pnlLeft_Create();
		$this->pnlRight_Create();
		$this->txtSearchAssets_Create();
		$this->btnSearchAssets_Create();
		$this->btnLendAssets_Create();
		$this->btnBorrowAssets_Create();
		$this->dtrAssets_Create();
		$this->dtrBasket_Create();
		$this->lnkEmptyBasket_Create();
		$this->dlgPeopleBox_Create();
		$this->lnkChoosePerson_Create();
		$this->lnkListView_Create();
		$this->lnkCoverView_Create();
	
		$this->LoadAssets();
		$this->LoadPeople();
		$this->pnlHeader_Create();
	}
	
	protected function lnkListView_Create()
	{
		$this->lnkListView = new QLinkButton($this);
		$this->lnkListView->Text ='List View';
		$this->lnkListView->AddAction(new QClickEvent(),new QServerAction('ListView_Click'));
		$this->lnkListView->SetCustomStyle('align','right');
	}
	
	protected function lnkCoverView_Create()
	{
		$this->lnkCoverView = new QLinkButton($this);
		$this->lnkCoverView->Text ='Cover View';
		$this->lnkCoverView->AddAction(new QClickEvent(),new QServerAction('CoverView_Click'));
		$this->lnkCoverView->SetCustomStyle('align','right');
	}
	
	protected function ListView_Click()
	{
		$this->blnListView = true;
		$this->blnListView = false;
		$this->dtrAssets->Template = 'pnlLibraryList.tpl.php';
		$this->pnlLeft->Refresh();
	}
	
	protected function CoverView_Click()
	{
		$this->blnListView = false;
		$this->dtrAssets->Template = 'pnlLibraryCover.tpl.php';
		$this->pnlLeft->Refresh();
	}
	
	protected function pnlLeft_Create()
	{	
		$this->pnlLeft = new QPanel($this);
		$this->pnlLeft->AutoRenderChildren = true;
		$this->pnlLeft->Visible = true;
	}
	
	protected function pnlRight_Create()
	{	
		$this->pnlRight = new QPanel($this);
		$this->pnlRight->AutoRenderChildren = true;
		$this->pnlRight->Visible = true;
	}
	
	protected function txtSearchAssets_Create()
	{
		$this->txtSearchAssets = new QTextBox($this->pnlLeft);
		$this->txtSearchAssets->Name = 'Search Library';
	}
	
	protected function txtSearchPeople_Create()
	{
		$this->txtSearchPeople = new QTextBox($this->pnlRight);
		$this->txtSearchPeople->Name = 'Search Contacts';
	}
	
	protected function btnSearchAssets_Create()
	{
		$this->btnSearchAssets = new QButton($this->pnlLeft);
		$this->btnSearchAssets->Text = 'Search';
		$this->btnSearchAssets->AddAction(new QClickEvent(),new QServerAction('AssetSearch_Click'));
	}
	
	protected function btnSearchPeople_Create()
	{
		$this->btnSearchPeople = new QButton($this->pnlRight);
		$this->btnSearchPeople->Text = 'Search';
		$this->btnSearchPeople->AddAction(new QClickEvent(),new QServerAction('PeopleSearch_Click'));
	}
	
	protected function btnLendAssets_Create()
	{
		$this->btnLendAssets = new QButton($this);
		$this->btnLendAssets->Text = 'Give your assets to ==> ';
		$this->btnLendAssets->AddAction(new QClickEvent(),new QServerAction('LendAssets'));
	}
	
	protected function lnkEmptyBasket_Create()
	{
		$this->lnkEmptyBasket = new QLinkButton($this);
		$this->lnkEmptyBasket->Text = 'Empty Basket';
		$this->lnkEmptyBasket->AddAction(new QClickEvent(),new QServerAction('EmptyBasket_Click'));
	}
	protected function btnBorrowAssets_Create()
	{
		$this->btnBorrowAssets = new QButton($this);
		$this->btnBorrowAssets->Text = 'Borrow assets from <==';
	}
	protected function LoadAssets()
	{
		$this->dtrAssets_Bind();	
	}
	
	protected function LoadPeople()
	{
		$this->dtrPeople_Bind();
	}
	
	protected function dtrAssets_Create()
	{
		$this->dtrAssets = new QDataRepeater($this->pnlLeft);
		$this->dtrAssets->Paginator = new QPaginator($this->pnlLeft);
		$this->dtrAssets->CssClass = 'datagrid';
		$this->dtrAssets->ItemsPerPage = 4;
		$this->dtrAssets->UseAjax = false;
		if($this->blnListView == true)
		{
			$this->dtrAssets->Template = 'pnlLibraryList.tpl.php';
		}
		else
		{
			$this->dtrAssets->Template = 'pnlLibraryCover.tpl.php';
		}
		$this->dtrAssets->SetDataBinder('dtrAssets_Bind');
	}
	
	protected function dtrBasket_Create()
	{
		$this->objBasket = new cl_Basket();
		$this->dtrBasket = new QDataRepeater($this->pnlRight);
		$this->dtrBasket->CssClass = 'datagrid';
		$this->dtrBasket->UseAjax = false;
		$this->dtrBasket->Template = 'pnlBasket.tpl.php';
		$this->dtrBasket->SetDataBinder('dtrBasket_Bind');
	}
	
	protected function dtrAssets_Bind()
	{
		$this->GetAssetsData();
	}
	
	protected function dtrBasket_Bind()
	{
			$this->dtrBasket->DataSource = $this->objBasket->GetBasket();
	}
	
	private function GetAssetsData()
	{
		$objClauses = array();
		array_push($objClauses, QQ::OrderBy(QQN::Myassets()->Title));
		if ($objClause = $this->dtrAssets->LimitClause)
		{
			array_push($objClauses, $this->dtrAssets->LimitClause);
		}
		if($this->txtSearchAssets->Text <> '')
		{
			$condition =	QQ::AndCondition(
							QQ::OrCondition(
					QQ::Like(QQN::Myassets()->Title,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Author,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Artist,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Actor,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Director,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Publisher,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Label,'%'. $this->txtSearchAssets->Text . '%'),
					QQ::Like(QQN::Myassets()->Isbn,'%'. $this->txtSearchAssets->Text . '%')
					),
					QQ::Equal(QQN::Myassets()->Owner,$_SESSION['User'])
					); 
		}
		else
		{
			$condition = QQ::AndCondition(QQ::NotEqual(QQN::Myassets()->Asin,''),QQ::Equal(QQN::Myassets()->Owner,$_SESSION['User']));
		}
			$arrResult = Myassets::QueryArray($condition,$objClauses);
			$this->dtrAssets->TotalItemCount = Myassets::QueryCount($condition);
			$this->dtrAssets->DataSource = $arrResult;
	}
	
	protected function dtrPeople_Create()
	{
		$this->dtrPeople = new QDataRepeater($this->pnlRight);
		$this->dtrPeople->Paginator = new QPaginator($this->pnlRight);
		$this->dtrPeople->CssClass = 'datagrid';
		$this->dtrPeople->ItemsPerPage = 7;
		$this->dtrPeople->UseAjax = false;
		$this->dtrPeople->Template = 'pnlPeople.tpl.php';
		$this->dtrPeople->SetDataBinder('dtrPeople_Bind');
	}
	
	protected function ChooseAssets_Click($strFormId,$strControlId,$strParameter)
	{
		$this->objBasket->AddToBasket(new cl_Item($strParameter));
	}

	protected function dtrPeople_Bind()
	{
		//$this->GetPeopleData();
		
	}
	
	protected function GetPeopleData()
	{
		$objClauses = array();
		array_push($objClauses, QQ::OrderBy(QQN::Peopledetails()->FullName));
		if ($objClause = $this->dtrPeople->LimitClause)
		{
			array_push($objClauses, $this->dtrPeople->LimitClause);
		}
		
		
		if($this->txtSearchPeople->Text <> '')
		{
			$condition = QQ::OrCondition(	
							QQ::Like(QQN::Peopledetails()->FullName,'%'. $this->txtSearchPeople->Text . '%'),
							QQ::Like(QQN::Peopledetails()->Email,'%'. $this->txtSearchPeople->Text . '%'),
							QQ::Like(QQN::Peopledetails()->Phone,'%'. $this->txtSearchPeople->Text . '%')
										);
			$result = Peopledetails::QueryArray($condition,$objClause);
			$this->dtrPeople->DataSource = $result;
			$this->dtrPeople->TotalItemCount = Peopledetails::QueryCount($condition);
		}
		else
		{
			$result = Peopledetails::QueryArray(QQ::All(),$objClause);
			$this->dtrPeople->DataSource = $result;
			$this->dtrPeople->TotalItemCount = Peopledetails::QueryCount(QQ::All());
		}

	}
	
	protected function PeopleSearch_Click()
	{
		
		$this->dtrPeople_Bind();
	}
	
	protected function AssetSearch_Click()
	{
		$this->dtrAssets_Bind();
	}
	
	protected function RemoveAssets_Click($strFormId,$strControlId,$strParameter)
	{
		$hashId = $strParameter;
		$this->objBasket->RemoveItemByHashId($hashId);
	}

	protected function EmptyBasket_Click()
	{
		$this->objBasket->EmptyBasket();
	}

	protected function dlgPeopleBox_Create()
	{
		$this->dlgPeopleBox = new PeopleWidget('',$this);
        $this->dlgPeopleBox->Width = '500px';
        $this->dlgPeopleBox->Height = '500px';
        $this->dlgPeopleBox->Overflow = QOverflow::Scroll;
        $this->dlgPeopleBox->Padding = '10px';
        $this->dlgPeopleBox->FontSize = '12px';
		$this->dlgPeopleBox->Visible = false;
	}
	
	protected function lnkChoosePerson_Create()
	{
		$this->lnkChoosePerson = new QLinkButton($this);
		$this->lnkChoosePerson->Text = 'Lend your stuff to...';
		$this->lnkChoosePerson->AddAction(new QClickEvent(),new QServerAction('Person_Click'));
	}
	
	protected function Person_Click()
	{
		$this->dlgPeopleBox->ShowDialogBox();
	}
	protected function AddClicked($strFormId,$strControlId,$strParameter)
	{
		$this->strBorrower = $strParameter;
		$this->dlgPeopleBox->HideDialogBox();
		$this->LendBasket();
		$objPerson = Peopledetails::QuerySingle(QQ::Equal(QQN::Peopledetails()->Id,$this->strBorrower));
		QApplication::DisplayAlert('You have given the items to ' . $objPerson->FullName);
		$this->objBasket->EmptyBasket();
		$this->dtrBasket->DataSource = $this->objBasket->GetBasket();
	}
	
	protected function LendBasket()
	{
		foreach($this->objBasket->GetBasket() as $item)
		{
			$objItem = Myassets::QuerySingle(QQ::Equal(QQN::Myassets()->Id,$item->Id));
			$objPerson = Peopledetails::QuerySingle(QQ::Equal(QQN::Peopledetails()->Id,$this->strBorrower));
			$objShareDetails = new Sharedetails();
			$objShareDetails->Asin = $objItem->Asin;
			$objShareDetails->Email = $objPerson->Email;
			
			/*Logic to get the return date*/
			$today = new QDateTime(QDateTime::Now);
			$daySpan = new QDateTimeSpan();
			$daySpan->AddDays(30);
			$returnDate = $today->Add($daySpan);
			
			$objShareDetails->TakenDate = QDateTime::Now(false);
			$objShareDetails->ReturnDate = $returnDate;
			$objShareDetails->Title = $objItem->Title;
			$objShareDetails->FullName = $objPerson->FullName;
			$objShareDetails->Save();
		} 
	}
	
	protected function pnlHeader_Create()
	{
		$this->pnlHeader = new pnlHeader($this, 'CloseEditPane');
	}

}

ShareAssets::Run('ShareAssets');
?>